package main

import (
	"context"
	"log"
	"time"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"golang.org/x/crypto/bcrypt"
)

// InitTestUser 初始化测试用户
func InitTestUser() {
	// 检查是否已存在测试用户
	var existingUser User
	err := collection.FindOne(context.TODO(), bson.M{"username": "admin"}).Decode(&existingUser)
	if err == nil {
		log.Println("测试用户已存在，跳过初始化")
		return
	}

	// 创建测试用户
	hashedPassword, err := bcrypt.GenerateFromPassword([]byte("admin123"), bcrypt.DefaultCost)
	if err != nil {
		log.Printf("密码加密失败: %v", err)
		return
	}

	testUser := User{
		ID:       primitive.NewObjectID(),
		Username: "admin",
		Email:    "admin@example.com",
		Password: string(hashedPassword),
		CreateAt: time.Now(),
		UpdateAt: time.Now(),
	}

	_, err = collection.InsertOne(context.TODO(), testUser)
	if err != nil {
		log.Printf("创建测试用户失败: %v", err)
		return
	}

	log.Println("测试用户创建成功:")
	log.Println("用户名: admin")
	log.Println("密码: admin123")
}
